# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=miniupnpc
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
_base_ver=2.1
_snap_date=20190824
pkgver=${_base_ver}.${_snap_date}
pkgrel=2
pkgdesc="A small UPnP client library/tool to access Internet Gateway Devices (mingw-w64)"
arch=('any')
url="http://miniupnp.free.fr"
license=('BSD')
options=('staticlibs')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-python3")
source=(https://miniupnp.tuxfamily.org/files/${_realname}-${pkgver}.tar.gz{,.sig}
        001-fix-static-library-name.patch
        002-python-module-build.patch)
sha256sums=('1fadefea0ecdceb3e35bb5d901f0db880c826740e93148ddfb087b4b4389e165'
            'SKIP'
            '06faa3e848bf575da7855cff11215e1622ea039e3e1f3ad3e1390f994809ab78'
            'cdf8f265e7edeccfabf67c5ebe4f10a56ecbd89fdb8f3bb9466c62243944caed')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-fix-static-library-name.patch
  patch -p1 -i ${srcdir}/002-python-module-build.patch
}

build() {
  [[ -d build-${MINGW_CHOST} ]] && rm -rf build-${MINGW_CHOST}
  cp -rf ${_realname}-${pkgver} build-${MINGW_CHOST}
  #mkdir -p build-${MINGW_CHOST}
  cd build-${MINGW_CHOST}
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DUPNPC_BUILD_SHARED=ON \
    -DUPNPC_BUILD_STATIC=ON \
    .

  make

  msg "Build Python extension..."
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setupmingw32.py build
}

package() {
  cd build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/man3/miniupnpc.3 \
    "${pkgdir}${MINGW_PREFIX}/share/man/man3/miniupnpc.3"
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  msg "Install Python${pyt} extension..."
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setupmingw32.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}"
}
